//==============================================================================
#include "CBackground_MPEG.h"

//==============================================================================
CBackground_MPEG::CBackground_MPEG() {
	MPEG_Movie = NULL;

	Surf_Temp = NULL;

	CurrentFrame = 0;
};

//==============================================================================
bool CBackground_MPEG::OnLoad(char* File) {
	if((MPEG_Movie = SMPEG_new(File, &MPEG_Info, 0)) == NULL) {
		return false;
	}

	strcpy(this->File, File);

	Surf_Temp = IMG_Load("./gfx/blank.jpg");

    SMPEG_setdisplay(MPEG_Movie, Surf_Temp, NULL, NULL);

    SMPEG_scaleXY(MPEG_Movie, 512, 512);

	SMPEG_renderFrame(MPEG_Movie, 1);

    Tex_Preview.OnLoad(Surf_Temp);

	return true;
};

//------------------------------------------------------------------------------
void CBackground_MPEG::OnRender() {
    if(!MPEG_Movie) {
        if(OnLoad(File) == false) {
            return;
        }
    }

    SMPEG_renderFrame(MPEG_Movie, CurrentFrame);

    SMPEG_getinfo(MPEG_Movie, &MPEG_Info);

    if(MPEG_Info.current_frame < CurrentFrame) {
        CurrentFrame = 0;
        SMPEG_renderFrame(MPEG_Movie, CurrentFrame);
    }

    Tex_Background.OnLoad(Surf_Temp);

    Tex_Background.RenderQuad(0, 0, 800, 600);

    if(FrameOldTime + 33.36f < SDL_GetTicks()) {
        FrameOldTime = SDL_GetTicks();
        CurrentFrame++;
    }
};

//------------------------------------------------------------------------------
void CBackground_MPEG::OnRender_Fade() {
    SMPEG_renderFrame(MPEG_Movie, CurrentFrame);

    Tex_Background.OnLoad(Surf_Temp);

    glColor4f(1, 1, 1, 0.5f);
    Tex_Background.RenderQuad(0, 0, 800, 600);
    glColor4f(1, 1, 1, 1);

    Alpha -= 0.01f;
}

//------------------------------------------------------------------------------
void CBackground_MPEG::OnCleanup() {
	if(MPEG_Movie) SMPEG_delete(MPEG_Movie);
	MPEG_Movie = NULL;
};

//==============================================================================
void CBackground_MPEG::FadeStart() {
    Alpha = 1.0f;
};

//==============================================================================
